define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'batchpay/lists/index' + location.search,
                    // add_url: 'batchpay/lists/add',
                    // edit_url: 'batchpay/lists/edit',
                    // del_url: 'batchpay/lists/del',
                    // multi_url: 'batchpay/lists/multi',
                    import_url: 'batchpay/lists/import',
                    table: 'batchpay_lists',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'platform_name', title: __('Platform_name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'title', title: __('Title'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'createtime', title: __('Createtime'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.datetime},
                        {field: 'ordersn', title: __('Ordersn'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'platform_lists_id', title: __('Platform_lists_id')},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"-1":__('Status -1'),"-2":__('Status -2')},
                            formatter: function (value,row) {
                                if (value === 0) return '<span style="color: #429bf3;font-weight: 600">' + __('Status 0') + '</span>';;
                                if (value === 1) return '<span style="color: #e7ad20;font-weight: 600" >' + __('Status 1') + '</span>';;
                                if (value === 2) return '<span style="color: #39ad1e;font-weight: 600" >' + __('Status 2') + '</span>';;
                                if (value === -1) return '<span style="color: #f40000;font-weight: 600" >' + __('Status -1') + '</span>';;
                                if (value === -2) return '<span style="color: #b673eb;font-weight: 600" >' + __('Status -2') + '</span>';;
                            }
                        },
                        {field: 'money', title: __('Money'), operate:'BETWEEN'},
                        {field: 'realmoney', title: __('Realmoney'), operate:'BETWEEN'},
                        {field: 'fee', title: __('Fee'), operate:'BETWEEN'},
                        {field: 'ratio', title: __('Ratio'), operate:'BETWEEN'},
                        {field: 'bankname', title: __('Bankname'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'bankcard', title: __('Bankcard'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'realname', title: __('Realname'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'bankopen', title: __('Bankopen'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'ispublic', title: __('Ispublic'), searchList: {"0":__('Ispublic 0'),"1":__('Ispublic 1')}, formatter: function (value, row, index) {
                                // 从 searchList 中获取对应的状态文本
                                return this.searchList[value] || value; // 如果找不到则返回原值
                            }},
                        {field: 'remark', title: __('Remark'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'callbackurl', title: __('Callbackurl'), operate: 'LIKE', formatter: Table.api.formatter.url},
                        {field: 'payplatform', title: __('Payplatform'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'platform', title: __('Platform'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'appid', title: __('Appid'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'operate', title: __('Operate'),
                            table: table,
                            buttons: [
                                {
                                    name: 'pass',
                                    title: __('提交中控审核？'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-check-circle-o',
                                    confirm: '提交中控审核？',
                                    url: 'batchpay/Process/pass',
                                    visible: function (row) {
                                        return row.status == 0; // 当status为0时显示
                                    },
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh');
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        return false;
                                    }
                                },
                                {
                                    name: 'refuse',
                                    title: __('拒绝'),
                                    classname: 'btn btn-xs btn-danger btn-magic btn-ajax',
                                    icon: 'fa fa-times-circle-o',
                                    confirm: '是否拒绝该笔代付？',
                                    url: 'batchpay/Process/refuse',
                                    visible: function (row) {
                                        return row.status == 0; // 当status为0时显示
                                    },
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh');
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        return false;
                                    }
                                },
                                {
                                    name: 'click',
                                    title: __('查询状态'),
                                    classname: 'btn btn-xs btn-info btn-click btn-ajax', // 添加 btn-ajax 类（如果框架依赖此类）
                                    icon: 'fa fa-leaf',
                                    refresh:true,
                                    url: 'batchpay/Process/check',
                                    click: function (data) {
                                        var self = this;
                                        // 手动发起Ajax请求
                                        $.ajax({
                                            url: self.url,
                                            type: 'GET', // 根据需求调整 GET/POST
                                            data: {},    // 可传递参数
                                            success: function (ret) {

                                            },
                                            error: function (xhr) {
                                                // Layer.alert("请求失败: " + xhr.responseText);
                                            }
                                        });
                                    }
                                },
                                {
                                    name: 'detail',
                                    title: __('代付详细信息'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-th-large',
                                    url: 'batchpay/Lists/detail',
                                },
                            ],
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate

                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
